Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

209
Visualizações
Change img [src] specifically with ngSwitch

I want to change img tag src based on a condition. I am using ternary operator right now; I want to ask is there a way to use ngSwitch instead (WITHOUT REPEATING the img tag). Here is my code:

<div>
  <img height='26' width='22' [src]="
       bank.bankName.includes('a') ? 'assets/images/a.png' :
       bank.bankName.includes('b') ? 'assets/images/b.png' :
       bank.bankName.includes('c') ? 'assets/images/c.png' :
       bank.bankName.includes('d') ? 'assets/images/d.png' : ''
  " />
</div> 
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

I would create a pipe, so you write once the code and then just add it to any HTML file, especially if you are going to use this feature in more components. Moreover, it is easier to modify/extend in the future, as you only have to modify in one place.

I leave HERE a stackblitz with the code, but it would be just something like this:

A) For using it in any HTML file (just add | bankNameImagePipe):

<img height='26' width='22' [src]="bank.bankName | bankNameImagePipe" />

B) Pipe file:

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'bankNameImagePipe',
})
export class BankNameImagePipe implements PipeTransform {

  transform(value: string): string {

    let urlBankImg: string = 'assets/images/';

    switch (value) {
      case 'a':
        urlBankImg+= 'a.png';
        break;
      case 'b':
        urlBankImg+= 'b.png';
        break;
      case 'c':
        urlBankImg+= 'c.png';
        break;
      case 'd':
        urlBanckImg+= 'd.png';
        break;

      default:
        urlBankImg= 'default.png'; // You can add a 'default' image url here
        break;
    }

    return urlBankImg;
  }
}

NOTE: I assume that bank.bankName will be a string and not an array. If it were not a string, you would have to change the pipe appropriately.

HOW TO CREATE THE PIPE? You only need to type this command in your console/bash to create a pipe:

ng g pipe pipes/bankNameImage

ng means Angular CLI g means Generate pipes means the folder where you want the pipe to be bankNameImage means the pipe name (the one to be used for using the pipeline in HTML)

Then, you only have to fill the empty template code that this command will have created in the file bank-name-image.pipe.ts with the code I provided above.

over 4 years ago · Santiago Trujillo Relatório

0

I'll try answering the question (Instead of telling you what would i do). No, you can't use ngSwitch instead without repeating the whole img tag. Each ngSwitchCase will display/hide an element. That's how the ngSwitch syntax works:

https://angular.io/api/common/NgSwitchCase

So, the syntax for your labelling with ngSwitch would be something like:

  <ng-container [ngSwitch]="bank.bankName">
    <img *ngSwitchCase="'a'" src="/assets/a.svg" />
    <img *ngSwitchCase="'b'" src="/assets/b.svg" />
    <img *ngSwitchCase="'c'" src="/assets/c.svg" />
    <img *ngSwitchCase="'d'" src="/assets/d.svg" />
  </ng-container>

I've left a crude example here:

https://stackblitz.com/edit/angular-ivy-lmfttq?file=src/app/hello.component.html

That is how ngSwitch works in Angular templates.

over 4 years ago · Santiago Trujillo Relatório

0

Writing code in a chain of ternary operators is never really a good move, especially in HTML templates. The switch is fine and if you really want to avoid it, you can simply write this code in component.ts file and set an public imageSrc = '' named peropty, compute the result and assign it.

<div>
  <img height='26' width='22' [src]="imageSrc" />
</div> 
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda